package pers.cz.chat;

import pers.cz.chat.client.conn.Session;
import pers.cz.netty.util.ContextHolder;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

/**
 * @program: Jef-Chat
 * @description:
 * @author: Cheng Zhi
 * @create: 2024-12-15 19:58
 **/
public class ClientApplication {

    static {
        System.setProperty("org.slf4j.simpleLogger.defaultLogLevel", "error");
    }

    public static void main(String[] args) throws InterruptedException {
        // 启动一个检测线程
        //checkUiDead();
        ContextHolder.initContext();
        Session session = new Session();
        session.start();
    }

    private static void checkUiDead() {
        new Thread(() -> {
           try {
               while (true) {
                   Process process = Runtime.getRuntime().exec("jps -l");
                   BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
                   String line;
                   boolean isDead = true;
                   while ((line = reader.readLine()) != null) {
                       if (line.contains("chat-ui")) {
                           Thread.sleep(1000 * 60L);
                           isDead = false;
                       }
                   }
                   reader.close();
                   if (isDead) {
                       System.exit(0);
                   }
               }
           } catch (IOException e) {
               e.printStackTrace();
           } catch (InterruptedException e) {
	           throw new RuntimeException(e);
           }
        }).start();
    }

}
